<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class users extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct() {
		parent::__construct();
		ini_set('session.cookie_httponly',1); 
		ini_set('session.use_only_cookies',1);
		$this->load->model('user_class');
		$this->load->model('topuser_class');
		$this->load->model('checklist_class');
		$this->load->model('points_class');
		$this->load->model('points_credits_class');
	}
	public function list_users(){
		$data=array();

		$post = $this->input->post();
		$userInfo = $this->user_class->list_users_data($post);
		if($userInfo!=0){
			foreach($userInfo as $key=>$value){
				$data['intID'][$key]=$value['intID'];
				$post['intID']=$value['intID'];
				$user = $this->topuser_class->get_user_points($post);
				$data['intPoints'][$key]="";
				if($user!=0){
					$data['intPoints'][$key]=$user['intPoints'];
				}				
				$data['strName'][$key]=$value['strName'];
				$data['dateBirthday'][$key]=$value['dateBirthday'];
				$data['intAge'][$key]=$value['intAge'];
				$data['strEmail'][$key]=$value['strEmail'];
				$data['strImgLink'][$key]=$value['strImgLink'];
			}
		}
		echo json_encode($data);
	}
	public function get_user_data(){
		$post = $this->input->post();
		$a = @split('%3D', @base64_decode($post['intID']));
		if($a[0]=='user_id'){
			$post['intID'] = $a['1'];
		}		
		$userInfo = $this->user_class->get_user_data($post);
		if($userInfo != 0){
			$data['intID'] = $userInfo['intID'];
			$post['intID']= $userInfo['intID'];
			$points = $this->points_class->list_points();
			if($points!=0){
				foreach($points as $key=>$value){
					$post['intPointType'] = $value['intID'];
					$data['strPointType'][$value['intID']] = $value['strTitle'];
					$user = $this->topuser_class->get_user_points($post);
					if($user!=0){
						if($key==1){
							$data['intPoints'] = $user['intPoints'];
						}
						if($value['intID']==2){
							$data['intAvailPoints'] = $user['intPoints'];
						}
						$data['intUserPoints'][$value['intID']] = $user['intPoints'];
					}				
				}
			}
			$data['strName'] = $userInfo['strName'];
			$data['dateBirthday'] = $userInfo['dateBirthday'];
			$data['strEmail'] = $userInfo['strEmail'];
			$data['strImgLink'] = $userInfo['strImgLink'];
			$points_credits = $this->points_credits_class->list_points_credits();
			$data['sysPoint'] = 0;
			$data['sysCredit'] = 0;
			if($points_credits!=0){
				foreach($points_credits as $key=>$value){
					if($data['sysPoint']==0){
						$data['sysPoint'] = $value['intPoints'];
						$data['sysCredit'] = $value['intCredits'];
					}
				}
			}
		}
		echo json_encode($data);
	}
	public function save_users(){
		$post = $this->input->post();
		$message = '';
		$users = $this->user_class->get_user_data($post);
		
		if($users!=0){
			$message = $this->user_class->update($post);
			$user1 = $this->topuser_class->get_user_points($post);
			//if($user1==0){
			//	$message = $this->user_class->add_user_points($post);
			//}
		}
		else {
			$message = $this->user_class->add_user($post);
		}
		echo json_encode($message);
	}
	public function add_user(){
		$post = $this->input->post();
		$userInfo = $this->user_class->add_user($post);
		echo json_encode($userInfo);
	}	
	public function change_perm_user(){
		$post = $this->input->post();
		$userInfo = $this->user_class->change_perm($post);
		echo json_encode($userInfo);
	}	
	public function delete_user(){
		$post = $this->input->post();
		$userInfo = $this->user_class->delete($post);
		echo json_encode($userInfo);
	}	

	public function get_user_checklist(){
		$post = $this->input->post();
		$userChecklist = $this->checklist_class->list_checklist_user($post);
		if($userChecklist != 0){
			foreach($userChecklist as $key=>$value){
				$data['intID'][$key] = $value['intID'];
				$data['strDescription'][$key] = $value['strDescription'];
				$data['dateCreatedDate'][$key] = $value['dateCreatedDate'];
			}
		}
		echo json_encode($data);
	}
	public function update_user_points(){
		$post = $this->input->post();
		$message['message'] = "Error";
		$a = @split('%3D', @base64_decode($post['intUserID']));
		if($a[0]=='user_id'){
			$post['intUserID'] = $a['1'];
			$post['intPointsID'] = 2; 
			$post['intPoints'] = $post['intAvailPoints']; 
			$message = $this->topuser_class->update_user_points($post);
			$post['intPointsID'] = 3; 
			$post['intPoints'] = $post['intCredit']; 
			$message = $this->topuser_class->update_user_points($post);			
		}		
		echo json_encode($message);
	}
	public function update_user_photo(){
		$post = $this->input->post();
		$message['message'] = "Error";
		$a = @split('%3D', @base64_decode($post['intUserID']));
		if($a[0]=='user_id'){
			$post['intID'] = $a['1'];
			$message = $this->user_class->update_user_photo($post);
		}
		echo json_encode($message);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */